﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class frm添加采购 : Form
    {
        public frm添加采购()
        {
            InitializeComponent();
        }
        string str = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
        private void frm添加采购_Load(object sender, EventArgs e)
        {
            chaxun();
            serachdanjuhao();
        }

        int danjuhao;
        public void serachdanjuhao()
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string sql1 = @"select danjuhaoid from dbo.单据号";
            SqlCommand com1 = new SqlCommand(sql1, conn);
            SqlDataReader rea1 = com1.ExecuteReader();
            while (rea1.Read())
            {
                danjuhao = Convert.ToInt32(rea1["danjuhaoid"]) + 1;
            }
        //    MessageBox.Show(danjuhao + "");

            rea1.Close();
            string sql2 = " insert into dbo.单据号 values('" + danjuhao + "',2)";
           SqlCommand comm=new SqlCommand(sql2,conn);
            int aaa = comm.ExecuteNonQuery();
     //       MessageBox.Show(aaa+"");
        }
     
        public void chaxun()
        {
            SqlConnection conn = new SqlConnection(str);
            DataSet ds = new DataSet("Student");
            string sql = @"select spID,spName,slLei,spJia from dbo.commodity,[Commodity type]
where commodity.spLei=[Commodity type].SlId 
 and spID like'%" + this.textBox1.Text + "%'";
            SqlDataAdapter asdf = new SqlDataAdapter(sql, conn);
            asdf.Fill(ds, "Student");
            dgvqw1.DataSource = ds.Tables["Student"];
        }
        //采购按钮
        private void btncaigou_Click(object sender, EventArgs e)
        {
            frm采购商品 frm = new frm采购商品();
            frm.bianhao =Convert.ToInt32( this.dgvqw1.SelectedCells[0].Value);
         // MessageBox.Show(frm.bianhao+"");
            frm.ShowDialog();
            sdf();
        }

        public void sdf() {
            SqlConnection con = new SqlConnection(str);
            DataSet ds = new DataSet("Student");
            string sql2 = @"select id,commid,danwei,sum,spName,spJia from dbo.sangping,dbo.commodity
where dbo.commodity.spID=sangping.commid and 单据号='" + danjuhao + "'";
            SqlDataAdapter asdf = new SqlDataAdapter(sql2, con);
            asdf.Fill(ds, "Student");
            this.dataGridView1.DataSource = ds.Tables["Student"];
        
        }
//增加新商品
        private void btnzengjia_Click(object sender, EventArgs e)
        {
            frm增加新商品 frm = new frm增加新商品();
            frm.ShowDialog();
            chaxun();
        }
        //退出
        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //右边查询代码
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           

        }
        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult reader = MessageBox.Show("确定删除吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (this.dgvqw1.SelectedCells.Count > 0)
            {
                string qwe = this.dataGridView1.SelectedCells[0].Value.ToString();
           //     MessageBox.Show(qwe);
                if (reader == DialogResult.OK)
                {
                    SqlConnection conn = new SqlConnection(str);
                    conn.Open();
                    try
                    {
                        string sql = "delete dbo.sangping where id='" + qwe + "'";
                        SqlCommand comm = new SqlCommand(sql, conn);
                        int i = comm.ExecuteNonQuery();
                        if (i > 0)
                        {
                            MessageBox.Show("删除成功");
                            sdf();
                        }
                        else
                        {
                            MessageBox.Show("重新选择");

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }

            }
        }

      
        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
          
        }

        private void button4_Click(object sender, EventArgs e)
        {

            this.Close();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            chaxun();
        }


    }
}
